﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

namespace Framework.Manager
{
    public class ResourceManager : ManagerBase<ResourceManager>
    {
        #region Manager通用
        private ResourceManager() { }
        #endregion

        //private Dictionary<string, Scene> mSceneDictionary = new Dictionary<string, Scene>();
        private Dictionary<string, AudioClip> mAudioClipDictionary = new Dictionary<string, AudioClip>();
        private Dictionary<string, GameObject> mGameObjectDictionary = new Dictionary<string, GameObject>();
        private Dictionary<string, TextAsset> mTextAssetDictionary = new Dictionary<string, TextAsset>();
        public Dictionary<string, Texture> mTextureDictionary = new Dictionary<string, Texture>();
        private Dictionary<string, Sprite> mSpriteDictionary = new Dictionary<string, Sprite>();

        #region 添加资源相关
        public void AddAsset(object pResult)
        {
            if (pResult is List<UnityEngine.Object>)
            {
                List<UnityEngine.Object> assetList = pResult as List<UnityEngine.Object>;
                for (int index = 0; index < assetList.Count; index++)
                {
                    AddAsset(assetList[index]);
                }
            }
            else
            {
                if (pResult is AudioClip)
                    AddAudioClip(pResult);
                else if (pResult is GameObject)
                    AddGameObject(pResult);
                else if (pResult is TextAsset)
                    AddTextAsset(pResult);
                else if (pResult is Texture)
                    AddTexture(pResult);
                else if (pResult is Sprite)
                    AddSprite(pResult);
            }
        }

        private void AddAudioClip(object pAssetObject)
        {
            AudioClip audioClip = pAssetObject as AudioClip;
            if (!mAudioClipDictionary.ContainsKey(audioClip.name))
                mAudioClipDictionary.Add(audioClip.name, audioClip);
        }

        private void AddGameObject(object pAssetObject)
        {
            GameObject gameObj = pAssetObject as GameObject;
            if (!mGameObjectDictionary.ContainsKey(gameObj.name))
                mGameObjectDictionary.Add(gameObj.name, gameObj);
        }

        private void AddTextAsset(object pAssetObject)
        {
            TextAsset textAsset = pAssetObject as TextAsset;
            if (!mTextAssetDictionary.ContainsKey(textAsset.name))
                mTextAssetDictionary.Add(textAsset.name, textAsset);
        }

        private void AddTexture(object pAssetObject)
        {
            Texture texture = pAssetObject as Texture;
            if (!mTextureDictionary.ContainsKey(texture.name))
                mTextureDictionary.Add(texture.name, texture);
        }

        private void AddSprite(object pAssetObject)
        {
            Sprite sprite = pAssetObject as Sprite;
            if (!mSpriteDictionary.ContainsKey(sprite.name))
                mSpriteDictionary.Add(sprite.name, sprite);
        }
        #endregion

        #region 移除资源相关
        public void RemoveAsset(object pResult)
        {
            if (pResult is List<UnityEngine.Object>)
            {
                List<UnityEngine.Object> assetList = pResult as List<UnityEngine.Object>;
                for (int index = 0; index < assetList.Count; index++)
                {
                    RemoveAsset(assetList[index]);
                }
            }
            else
            {
                if (pResult is AudioClip)
                    RemoveAudioClip(pResult);
                else if (pResult is GameObject)
                    RemoveGameObject(pResult);
                else if (pResult is TextAsset)
                    RemoveTextAsset(pResult);
                else if (pResult is Texture)
                    RemoveTexture(pResult);
                else if (pResult is Sprite)
                    RemoveSprite(pResult);
            }
        }

        private void RemoveAudioClip(object pAssetObject)
        {
            AudioClip audioClip = pAssetObject as AudioClip;
            if (mAudioClipDictionary.ContainsKey(audioClip.name))
                mAudioClipDictionary.Remove(audioClip.name);
        }

        private void RemoveGameObject(object pAssetObject)
        {
            GameObject gameObj = pAssetObject as GameObject;
            if (mGameObjectDictionary.ContainsKey(gameObj.name))
                mGameObjectDictionary.Remove(gameObj.name);
        }

        private void RemoveTextAsset(object pAssetObject)
        {
            TextAsset textAsset = pAssetObject as TextAsset;
            if (mTextAssetDictionary.ContainsKey(textAsset.name))
                mTextAssetDictionary.Remove(textAsset.name);
        }

        private void RemoveTexture(object pAssetObject)
        {
            Texture texture = pAssetObject as Texture;
            if (mTextureDictionary.ContainsKey(texture.name))
                mTextureDictionary.Remove(texture.name);
        }

        private void RemoveSprite(object pAssetObject)
        {
            Sprite sprite = pAssetObject as Sprite;
            if (mSpriteDictionary.ContainsKey(sprite.name))
                mSpriteDictionary.Remove(sprite.name);
        }
        #endregion

        #region 获取资源
        public AudioClip GetAudioClip(string pAudioClipName)
        {
            if (mAudioClipDictionary.ContainsKey(pAudioClipName))
                return mAudioClipDictionary[pAudioClipName];
            else
                return null;
        }

        public GameObject GetGameObject(string pGameObjectName)
        {
            if (mGameObjectDictionary.ContainsKey(pGameObjectName))
                return mGameObjectDictionary[pGameObjectName];
            else
                return null;
        }

        public async Task<T> GetInstantiate<T>(string pGameObjectName, Transform pParent = null)
        {
            GameObject resourceGameObject = GetGameObject(pGameObjectName);
            if (resourceGameObject == null)
            {
                bool result = await ManagerCollection.LoadManager.LoadAssetAsync(pGameObjectName);
                if (result)
                    return await GetInstantiate<T>(pGameObjectName, pParent);
            }
            else
            {
                GameObject instantiateGameObject = GameObject.Instantiate(resourceGameObject, pParent);
                T instantiate = instantiateGameObject.GetComponent<T>();
                return instantiate;
            }

            return default(T);
        }

        public TextAsset GetTextAsset(string pTextAssetName)
        {
            if (mTextAssetDictionary.ContainsKey(pTextAssetName))
                return mTextAssetDictionary[pTextAssetName];
            else
                return null;
        }

        public Texture GetTexture(string pTextureName)
        {
            if (mTextureDictionary.ContainsKey(pTextureName))
                return mTextureDictionary[pTextureName];
            else
                return null;
        }

        public Sprite GetSprite(string pSpriteName)
        {
            if (mSpriteDictionary.ContainsKey(pSpriteName))
                return mSpriteDictionary[pSpriteName];
            else
            {
                Texture texture = GetTexture(pSpriteName);
                if (texture)
                    return Sprite.Create(texture as Texture2D, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
                else
                    return null;
            }
        }
        #endregion
    }
}